home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / src / arcs.c < prev    next >
C/C++ Source or Header  |  1997-08-17  |  8KB  |  427 lines

  1. #include "vogl.h"
  2.  
  3. #ifdef    TC
  4.  
  5. extern    double    cos();
  6. extern    double    sin();
  7.  
  8. #else 
  9.  
  10. #include <math.h>
  11.  
  12. #endif
  13.  
  14. static int    nsegs = 32;
  15.  
  16. /*
  17.  * arcprecision
  18.  *
  19.  *    sets the number of segments in an arc or circle.
  20.  *    - obsolete function.
  21.  */
  22. void arcprecision(int noseg)
  23. {
  24. nsegs = noseg;
  25. }
  26.  
  27. /* ------------------------------------------------------------------------ */
  28.  
  29. /*
  30.  * circleprecision
  31.  *
  32.  *    sets the number of segments in an arc or circle.
  33.  */
  34. void circleprecision(int noseg)
  35. {
  36. nsegs = noseg;
  37. }
  38.  
  39. /* ------------------------------------------------------------------------ */
  40.  
  41. /*
  42.  * arc
  43.  *
  44.  * draw an arc at a given location.  Precision of arc (# line segments)
  45.  * is calculated from the value given to circleprecision.
  46.  *
  47.  */
  48. void arc(
  49.   Coord x,
  50.   Coord y,
  51.   Coord radius,
  52.   Angle sang,
  53.   Angle eang)
  54. {
  55. Token    *t;
  56. float    cx, cy, dx, dy;
  57. float    startang, endang, deltang, cosine, sine, angle;
  58. int    i, numsegs;
  59.  
  60. if (!vdevice.initialised)
  61. verror("arc: vogl not initialised");
  62.  
  63. startang = (float)sang / 10.0;
  64. endang = (float)eang / 10.0;
  65.  
  66. angle = startang * D2R;
  67. numsegs = (int)((endang - startang) / 360.0 * nsegs + 0.5);
  68. deltang = (endang - startang) * D2R / numsegs;
  69. cosine = cos((double)deltang);
  70. sine = sin((double)deltang);
  71.  
  72. if (vdevice.inobject) {
  73.     t = newtokens(8);
  74.     t[0].i = ARC;
  75.     t[1].f = x;
  76.     t[2].f = y;
  77.     t[3].f = radius * cos((double)angle);
  78.     t[4].f = radius * sin((double)angle);
  79.     t[5].f = cosine;
  80.     t[6].f = sine;
  81.     t[7].i = numsegs;
  82.     return;
  83.     }
  84.  
  85. /* calculates initial point on arc */
  86.  
  87. cx = x + radius * cos((double)angle);
  88. cy = y + radius * sin((double)angle);
  89. move2(cx, cy);
  90.  
  91. for (i = 0; i < numsegs; i++) {
  92.     dx = cx - x; 
  93.     dy = cy - y;
  94.     cx = x + dx * cosine - dy * sine;
  95.     cy = y + dx * sine + dy * cosine;
  96.     draw2(cx, cy);
  97.     }
  98. }
  99.  
  100. /* ------------------------------------------------------------------------ */
  101.  
  102. /*
  103.  * arcs
  104.  *
  105.  * draw an arc at a given location.  (Expressed as short integers) 
  106.  * Precision of arc (# line segments) is calculated from the value
  107.  * given to circleprecision.
  108.  *
  109.  */
  110. void arcs(
  111.   Scoord x,
  112.   Scoord y,
  113.   Scoord radius,
  114.   Angle sang,
  115.   Angle eang)
  116. {
  117. arc((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  118. }
  119.  
  120. /* ------------------------------------------------------------------------ */
  121.  
  122. /*
  123.  * arci
  124.  *
  125.  * draw an arc at a given location.  (Expressed as integers) 
  126.  * Precision of arc (# line segments) is calculated from the value
  127.  * given to circleprecision.
  128.  *
  129.  */
  130. void arci(
  131.   Icoord x,
  132.   Icoord y,
  133.   Icoord radius,
  134.   Angle sang,
  135.   Angle eang)
  136. {
  137. arc((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  138. }
  139.  
  140. /* ------------------------------------------------------------------------ */
  141.  
  142. /*
  143.  * arcf
  144.  *
  145.  *    draw a filled sector in a given location. The number of line
  146.  * segments in the arc of the segment is the same as in arc.
  147.  */
  148. void arcf(
  149.   Coord x,
  150.   Coord y,
  151.   Coord radius,
  152.   Angle sang,
  153.   Angle eang)
  154. {
  155. Token    *t;
  156. float    cx, cy, dx, dy;
  157. float    deltang, cosine, sine, angle;
  158. int    i, numsegs;
  159. float    startang, endang;
  160.  
  161. if (!vdevice.initialised)
  162. verror("arcf: vogl not initialised");
  163.  
  164. startang = sang / 10.0;
  165. endang = eang / 10.0;
  166.  
  167. angle = startang * D2R;
  168. numsegs = (int)((endang - startang) / 360.0 * nsegs + 0.5);
  169. deltang = (endang - startang) * D2R / numsegs;
  170. cosine = cos((double)deltang);
  171. sine = sin((double)deltang);
  172.  
  173. if (vdevice.inobject) {
  174.     t = newtokens(8);
  175.     t[0].i = ARCF;
  176.     t[1].f = x;
  177.     t[2].f = y;
  178.     t[3].f = radius * cos((double)angle);
  179.     t[4].f = radius * sin((double)angle);
  180.     t[5].f = cosine;
  181.     t[6].f = sine;
  182.     t[7].i = numsegs;
  183.     return;
  184.     }
  185.  
  186. pmv2(x, y);
  187. /* calculates initial point on arc */
  188.  
  189. cx = x + radius * cos((double)angle);
  190. cy = y + radius * sin((double)angle);
  191.  
  192. pdr2(cx, cy);
  193.  
  194. for (i = 0; i < numsegs; i++) {
  195.     dx = cx - x; 
  196.     dy = cy - y;
  197.     cx = x + dx * cosine - dy * sine;
  198.     cy = y + dx * sine + dy * cosine;
  199.     pdr2(cx, cy);
  200.     }
  201.  
  202. pclos();
  203. }
  204.  
  205. /* ------------------------------------------------------------------------ */
  206.  
  207. /*
  208.  * arcfs
  209.  *
  210.  * draw a filled sector at a given location.  (Expressed as short integers) 
  211.  * Precision of arc (# line segments) is calculated from the value
  212.  * given to circleprecision.
  213.  *
  214.  */
  215. void arcfs(
  216.   Scoord x,
  217.   Scoord y,
  218.   Scoord radius,
  219.   Angle sang,
  220.   Angle eang)
  221. {
  222. arcf((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  223. }
  224.  
  225. /* ------------------------------------------------------------------------ */
  226.  
  227. /*
  228.  * arcfi
  229.  *
  230.  * draw a filled sector at a given location.  (Expressed as integers) 
  231.  * Precision of arc (# line segments) is calculated from the value
  232.  * given to circleprecision.
  233.  *
  234.  */
  235. void arcfi(
  236.   Icoord x,
  237.   Icoord y,
  238.   Icoord radius,
  239.   Angle sang,
  240.   Angle eang)
  241. {
  242. arcf((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  243. }
  244.  
  245. /* ------------------------------------------------------------------------ */
  246.  
  247.  
  248. /*
  249.  * circ
  250.  *
  251.  * Draw a circle of given radius at given world coordinates. The number of
  252.  * segments in the circle is the same as that of an arc.
  253.  *
  254.  */
  255. void circ(
  256.   Coord x,
  257.   Coord y,
  258.   Coord radius)
  259. {
  260. Token    *t;
  261. float    cx, cy, dx, dy;
  262. float    angle, cosine, sine;
  263. int    i;
  264.  
  265. if (!vdevice.initialised)
  266. verror("circ: vogl not initialised");
  267.  
  268. angle = 2.0 * PI / nsegs;
  269. cosine = cos((double)angle);
  270. sine = sin((double)angle);
  271.  
  272. if (vdevice.inobject) {
  273.     t = newtokens(7);
  274.     t[0].i = CIRCLE;
  275.     t[1].f = x;
  276.     t[2].f = y;
  277.     t[3].f = radius;
  278.     t[4].f = cosine;
  279.     t[5].f = sine;
  280.     t[6].i = nsegs;
  281.     return;
  282.     }
  283.  
  284. cx = x + radius;
  285. cy = y;
  286.  
  287. move2(cx, cy);
  288. for (i = 0; i < nsegs - 1; i++) {
  289.     dx = cx - x; 
  290.     dy = cy - y;
  291.     cx = x + dx * cosine - dy * sine;
  292.     cy = y + dx * sine + dy * cosine;
  293.     draw2(cx, cy);
  294.     }
  295.  
  296. draw2(x + radius, y);
  297. }
  298.  
  299. /* ------------------------------------------------------------------------ */
  300.  
  301. /*
  302.  * circs
  303.  *
  304.  * Draw a circle of given radius at given world coordinates expressed as
  305.  * short integers. The number of segments in the circle is the same as that
  306.  * of an arc.
  307.  *
  308.  */
  309. void circs(
  310.   Scoord x,
  311.   Scoord y,
  312.   Scoord radius)
  313. {
  314. circ((Coord)x, (Coord)y, (Coord)radius);
  315. }
  316.  
  317. /* ------------------------------------------------------------------------ */
  318.  
  319.  
  320. /*
  321.  * circi
  322.  *
  323.  * Draw a circle of given radius at given world coordinates expressed as
  324.  * integers. The number of segments in the circle is the same as that
  325.  * of an arc.
  326.  *
  327.  */
  328. void circi(
  329.   Icoord x,
  330.   Icoord y,
  331.   Icoord radius)
  332. {
  333. circ((Coord)x, (Coord)y, (Coord)radius);
  334. }
  335.  
  336. /* ------------------------------------------------------------------------ */
  337.  
  338. /*
  339.  * circf
  340.  *
  341.  * Draw a filled circle of given radius at given world coordinates.
  342.  * The number of segments in the circle is the same as that of an arc.
  343.  *
  344.  */
  345. void circf(
  346.   Coord x,
  347.   Coord y,
  348.   Coord radius)
  349. {
  350. Token    *t;
  351. float    cx, cy, dx, dy;
  352. float    angle, cosine, sine;
  353. int    i;
  354.  
  355. if (!vdevice.initialised)
  356. verror("circf: vogl not initialised");
  357.  
  358. angle = 2.0 * PI / nsegs;
  359. cosine = cos((double)angle);
  360. sine = sin((double)angle);
  361.  
  362. if (vdevice.inobject) {
  363.     t = newtokens(7);
  364.     t[0].i = CIRCF;
  365.     t[1].f = x;
  366.     t[2].f = y;
  367.     t[3].f = radius;
  368.     t[4].f = cosine;
  369.     t[5].f = sine;
  370.     t[6].i = nsegs;
  371.     return;
  372.     }
  373.  
  374. cx = x + radius;
  375. cy = y;
  376.  
  377. pmv2(cx, cy);
  378. for (i = 0; i < nsegs - 1; i++) {
  379.     dx = cx - x; 
  380.     dy = cy - y;
  381.     cx = x + dx * cosine - dy * sine;
  382.     cy = y + dx * sine + dy * cosine;
  383.     pdr2(cx, cy);
  384.     }
  385.  
  386. pclos();
  387. }
  388.  
  389. /* ------------------------------------------------------------------------ */
  390.  
  391. /*
  392.  * circfs
  393.  *
  394.  * Draw a circle of given radius at given world coordinates expressed as
  395.  * short integers. The number of segments in the circle is the same as that
  396.  * of an arc.
  397.  *
  398.  */
  399. void circfs(
  400.   Scoord x,
  401.   Scoord y,
  402.   Scoord radius)
  403. {
  404. circf((Coord)x, (Coord)y, (Coord)radius);
  405. }
  406.  
  407. /* ------------------------------------------------------------------------ */
  408.  
  409. /*
  410.  * circfi
  411.  *
  412.  * Draw a circle of given radius at given world coordinates expressed as
  413.  * integers. The number of segments in the circle is the same as that
  414.  * of an arc.
  415.  *
  416.  */
  417. void circfi(
  418.   Icoord x,
  419.   Icoord y,
  420.   Icoord radius)
  421. {
  422. circf((Coord)x, (Coord)y, (Coord)radius);
  423. }
  424.  
  425. /* ------------------------------------------------------------------------ */
  426.  
  427.